#ifndef __FILE_PROTOCOL_ENGINE_H__
#define __FILE_PROTOCOL_ENGINE_H__ 1

#include <fstream>
#include <string>

#include "types.h"
#include "stegio.h"
#include "properties_reader.h"

#define LENGTH_FIELD_LENGTH 4


/**
 * @brief protocol engine for transmission of file payload.
 * @author gpitsch and rpoisel
 * @note capable to operate in full duplex
 *
 * file length (32 bit word in Network byte order)
 * is transmitted before file payload
 */
class CFileProtocolEngine : public IStegIO
{
public:

	/**
	 * @param pFilenameRead file containing the data that is @b sent
	 * @param pFilenameWrite file containing the data that is @b received
	 */
	CFileProtocolEngine(CPropertiesReader* pConfig);
	virtual ~CFileProtocolEngine();

	virtual bool open();
	virtual void close();
	virtual NStegIO::EReadState read(unsigned char& pByte);
	virtual void write(unsigned char pByte);
	virtual int available();

	static const std::string sDefaultReadFilename;
	static const std::string sDefaultWriteFilename;

private:
	CFileProtocolEngine();
	CFileProtocolEngine(const CFileProtocolEngine& );
	CFileProtocolEngine& operator=(const CFileProtocolEngine& );

	static const std::string sIniSectionName;

	CPropertiesReader* mConfig;

	std::string mIOFilenameRead;
	std::string mIOFilenameWrite;
	std::ifstream mIOStreamRead;
	std::ofstream mIOStreamWrite;

	bool mSendEof;

	int32_t mBytesToRead; // read process
	int32_t mBytesToReceive; // write process
	unsigned int mAvailableReadBytes; // for available() methode

	unsigned char mByteLengthBufferRead[LENGTH_FIELD_LENGTH];
	unsigned char mByteLengthBufferWrite[LENGTH_FIELD_LENGTH];
	unsigned char mLengthCntRead;
	unsigned char mLengthCntWrite;
};

#endif // __FILE_PROTOCOL_ENGINE_H__

